package com.xhdx;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapTest {
    public static void main(String args[]){
        Map<String,String> map = new HashMap<String, String>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");
        
        //NO.1 遍历
        System.out.println("NO.1 遍历,map.keySet()");
        for(String key : map.keySet()){
            System.out.println("key:" + key + ",value=" + map.get(key));
        }
        //NO.2 遍历
        System.out.println("NO.2 遍历,iterator");
        Iterator<Map.Entry<String,String>> iterator = map.entrySet().iterator();
        while(iterator.hasNext()){
            Map.Entry<String,String> entry= iterator.next();
            System.out.println("key:" + entry.getKey() + ",value=" + entry.getValue());
        }
        //NO.3 遍历,推荐使用，尤其是容量大
        System.out.println("NO.3 遍历,map.entrySet()");
        for(Map.Entry<String,String> entry : map.entrySet()){
            System.out.println("key:" + entry.getKey() + ",value=" + entry.getValue());
        }
        //NO.4 遍历
        System.out.println("NO.4 遍历,map.values(),只能取到value值, 取不到key值");
        for(String value : map.values()){
            System.out.println("value=" + value);
        }
        //NO.5 遍历
        System.out.println("NO.5 遍历,Lambda");
        map.forEach((key, value)->{
            System.out.println("key:" + key + ",value=" + value);
        });
    }
}
